<!DOCTYPE html>
<html lang="zh-cmn-Hans">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0,viewport-fit=cover">
    <title>上传防溺水防控人员照片</title>
    <link rel="stylesheet" href="./style/weui.css"/>
    <link rel="stylesheet" href="./style/example.css"/>
    <link rel="stylesheet" href="./style/jquery-weui.min.css">
    <link rel="stylesheet" href="./style/my.css">
    <link rel="stylesheet" type="text/css" href="./static/mobile/css/wx/style.css" />
    <link rel="stylesheet" type="text/css" href="./static/assets/layui/css/layui.css" />
    <style>
        /*.weui-cells{
            background: #fff !important;
        }*/
    </style>
</head>
<body>
<div class="page js_show home">
    <div style="font-size:30px;text-align: center;margin-top:20px;margin-bottom: 20px;">
        上传防溺水防控人员照片
    </div>
    <div class="weui-form__control-area">
        <div class="weui-cells__group weui-cells__group_form">
            <div class="weui-cells weui-cells_form">
                <div class="weui-cell weui-cell_active">
                    <div class="weui-cell__hd"><label class="weui-label">姓名:</label></div>
                    <div class="weui-cell__bd">
                        <input id="name" class="weui-input" placeholder="请填写姓名" type="text" value=""/>
                    </div>
                </div>
                <div class="weui-cell weui-cell_active">
                    <div class="weui-cell__hd"><label class="weui-label">联系电话</label></div>
                    <div class="weui-cell__bd">
                        <input id="tel" class="weui-input" placeholder="请填写联系电话" value="" type="number" pattern="[0-9]*"/>
                    </div>
                </div>
                <!-- <div class="weui-cell weui-cell_active">
                    <div class="weui-cell__hd"><label class="weui-label">上传照片：</label></div>
                    <div class="weui-cell__bd">
                        <div class="weui-uploader__bd">
                            <ul class="weui-uploader__files" id="uploaderFiles" wah-hotarea="click"></ul>
                            <div class="weui-uploader__input-box">
                                <input id="uploaderInput" class="weui-uploader__input" type="file" accept="image/jpg,image/jpeg" multiple="">
                            </div>
                        </div>
                    </div>
                    <div id="gallery" class="weui-gallery" style="display: none;">
                        <span class="weui-gallery__img" id="galleryImg"></span>
                        <div class="weui-gallery__opr">
                            <a href="javascript:" class="weui-gallery__del">
                                <i class="weui-icon-delete weui-icon_gallery-delete"></i>
                            </a>
                        </div>
                    </div>
                </div> -->
                <div class="weui-cell weui-cell_active">
                    <div class="weui-cell__hd"><label class="weui-label">上传照片：</label></div>
                    <div class="weui-cell__bd" style="display: block;">
                        <div class="device_detail" style="min-height:200px">
                                <div style="width:100px">
                                    <button type="button" class="layui-btn layui-btn-normal" style="width: 100%;border-radius: 20px;" id="uploadBtn">上传图片</button>
                                </div>
                                <div class="layui-upload" style="width: 100px;">
                                <div class="layui-upload-list" id="demo1" style="width: 100%; height:100px;">
                                    <img src="" height="100" style=" display: none;" />
                                    <input type="hidden" id="thumb_pic" name="thumb" value="">
                                </div>
                                
                            
                        </div>
                    </div>
                    
                </div>
                
            </div>
            <div style="color:red; font-size: 14px;margin-left:20px;margin-top:-40px">
                    建议照片使用1寸电子照片，小于500K。
                </div>
            <div class="button-sp-area" style="margin-top:65px;">
                <a href="javascript:" class="weui-btn weui-btn_primary" id="weui-btn_update">提交</a>
            </div>
        </div>
    </div>
</div>

<script src="./js/jquery.min.js"></script>
<script src="./js/jquery-weui.min.js"></script>
<script src="./js/zepto.min.js?a=1"></script>
<script src="./js/jweixin-1.0.0.js"></script>
<script src="./js/weui.min.js"></script>
<!-- <script src="./js/app.js"></script> -->
<script src="./static/mobile/js/jquery2.min.js"></script>

<script src="./static/mobile/js/layer/layer.js"></script>
<script src="./static/assets/layui/layui.js"></script>
<script src="./static/assets/gougu/gouguInit.js"></script>
<script src='./static/mobile/js/wx/jweixin-1.4.0.js'></script>
<script src='./static/mobile/js/wx/recharge.js?v=2'></script>
<script src="./static/mobile/js/wx/jquery.cookie.js"></script>
<script>
    const moduleInit = ['tool'];
            function gouguInit() {
                var form = layui.form,tool=layui.tool,upload = layui.upload;
                //头像上传
                //头像上传
                var uploadInst = upload.render({
                    elem: '#uploadBtn'
                    , url: 'http://60.217.22.201:8501/api/video/upload'
                    ,before: function(obj){ //上传前的回调
                    // 显示加载效果
                        layer.load(1);
                    }
                    , done: function (e) {

                        //如果上传失败
                        if (e.code == 1) {
                            return layer.msg('上传失败');
                        }
                        layer.msg('上传成功');
                        $('#demo1 img').show();
                        //上传成功
                        $('#demo1 input').attr('value', 'http://60.217.22.201:8501'+e.data.filepath);
                        $('#demo1 img').attr('src', 'http://60.217.22.201:8501'+e.data.filepath);
                        layer.closeAll('loading');
                    }
                    
                });
                

                /*//监听提交
                form.on('submit(webform)', function (data) {
                    let callback = function (e) {
                        layer.msg(e.msg);
                        if (e.code == 0) {
                            parent.layui.tool.close(1000);
                        }
                    }
                    tool.post("/admin/admin/add", data.field, callback);
                    return false;
                });*/
                $('#weui-btn_update').on('click',function () {
                    //alert()
                    /*var formData = new FormData();

                    // 获取文件输入元素
                    var fileInput = document.getElementById('thumb_pic');
                    var file = fileInput.files[0];

                    // 将文件添加到 FormData 对象
                    formData.append('file', file);

                    // 创建 JSON 数据
                    var jsonData = {
                        key1: 'value1',
                        key2: 'value2'
                    };

                    // 将 JSON 数据添加到 FormData 对象
                    formData.append('jsonData', JSON.stringify(jsonData));*/

                    var name = $("#name").val();
                    console.log(name.length);
                    if(name.length <= 0 || name.length > 8){
                        layer.msg('请检查姓名');
                        return false;
                    }
                    var tel = $("#tel").val();
                    if(tel.length <= 0 || tel.length != 11){
                        layer.msg('请检查联系方式');
                        return false;
                    }
                    var img = $('#thumb_pic').val();
                    if(img.length <= 0 ){
                        layer.msg('请检查图片');
                        return false;
                    }
                    let index = layer.load(1);
                    $.ajax({
                        url: 'http://60.217.22.201:8501/api/video/addface', // 后端接口 URL
                        type: 'POST',
                        data: {
                            name:name,
                            contact:tel,
                            face_img:img,
                        },
                        success: function(res) {
                            layer.msg(res.msg);
                            if(res.code==0){
                                location.href="success.html";
                            }else{
                                //location.href="warn.html";
                            }
                            console.log('成功:', res);
                            layer.close(index);

                        }/*,
                        error: function(error) {
                            layer.msg(error.msg);
                            console.error('失败:', error);
                            //location.href="warn.html";
                            layer.close(index);
                        }*/
                    });
                });
                /*$('.search_button').on('click',function () {
                    var keyword = document.getElementById("keyword").value;
                    var map = new BMapGL.Map("container");          
                    map.centerAndZoom(new BMapGL.Point(116.404, 39.915), 11);
                    map.enableScrollWheelZoom(true);
                    var local = new BMapGL.LocalSearch(map, {
                        renderOptions:{map: map}
                    });
                    local.search(keyword);
                    map.addEventListener('click', function (e) {
                        $(".container").hide();
                        $(".tosearch").hide();
                        $(".page-container").show();
                        $(".jingwei").html(e.latlng.lng + ',' + e.latlng.lat);
                        var marker1 = new BMapGL.Marker(new BMapGL.Point(e.latlng.lng, e.latlng.lat));
                        //alert('点击位置经纬度：' + e.latlng.lng + ',' + e.latlng.lat);
                    });
                });*/
            }
</script>
<script type="text/javascript" class="uploader js_show">
    /*$(function(){
        var tmpl = '<li class="weui-uploader__file" role="img" tabindex="0" style="background-image:url(#url#)"></li>',
            $gallery = $("#gallery"), $galleryImg = $("#galleryImg"),
            $uploaderInput = $("#uploaderInput"),
            $uploaderFiles = $("#uploaderFiles");

        $uploaderInput.on("change", function(e){
            var src, url = window.URL || window.webkitURL || window.mozURL, files = e.target.files;
            console.log(files);
            for (var i = 0, len = files.length; i < len; ++i) {
                var file = files[i];

                if (!file.type.match('image/jpeg') && !file.type.match('image/jpg')) {
                    alert('只能上传 jpeg 或 jpg 格式的图片');
                    continue;
                }

                if (url) {
                    src = url.createObjectURL(file);
                } else {
                    src = e.target.result;
                }
                compressImage(file, function(compressedSrc, compressedFile) {
                    console.log(compressedSrc);
                    console.log(compressedFile);
                    if (compressedFile.size <= 2 * 1024 * 1024) {
                        $uploaderFiles.append($(tmpl.replace('#url#', compressedSrc)));
                        uploadImage(compressedFile);
                    } else {
                        alert('图片大小不能超过 2MB');
                    }
                });
            }
        });

        var currentImg;
        $uploaderFiles.on("click", "li", function(){
            $galleryImg.attr("style", this.getAttribute("style"));
            $gallery.attr('aria-hidden','false');
            $gallery.attr('aria-modal','true');
            $gallery.fadeIn(100);
            setTimeout(function(){
              $galleryImg.attr("tabindex", '-1').trigger('focus');
            },200);
            currentImg = this;
        });
        $gallery.on("click", function(){
            $gallery.attr('aria-modal','false');
            $gallery.attr('aria-hidden','true');
            $gallery.fadeOut(100);
            setTimeout(function(){
              $galleryImg.removeAttr("tabindex");
            },200);
            currentImg.focus();
        });

        function compressImage(file, callback) {
            var reader = new FileReader();
            reader.onload = function(event) {
                var img = new Image();
                img.onload = function() {
                    var canvas = document.createElement('canvas');
                    var ctx = canvas.getContext('2d');
                    var width = img.width;
                    var height = img.height;

                    if (width > 1024 || height > 1024) {
                        if (width > height) {
                            height *= 1024 / width;
                            width = 1024;
                        } else {
                            width *= 1024 / height;
                            height = 1024;
                        }
                    }

                    canvas.width = width;
                    canvas.height = height;
                    ctx.drawImage(img, 0, 0, width, height);

                    var compressedSrc = canvas.toDataURL('image/jpeg', 0.8);
                    var compressedFile = dataURItoBlob(compressedSrc);

                    var faceWidth = 60;
                    var faceHeight = 60;
                    if (width < faceWidth || height < faceHeight) {
                        alert('人脸像素不能低于 60x60');
                        return;
                    }

                    callback(compressedSrc, compressedFile);
                };
                img.src = event.target.result;
            };
            reader.readAsDataURL(file);
        }

        function dataURItoBlob(dataURI) {
            var byteString = atob(dataURI.split(',')[1]);
            var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
            var ab = new ArrayBuffer(byteString.length);
            var ia = new Uint8Array(ab);
            for (var i = 0; i < byteString.length; i++) {
                ia[i] = byteString.charCodeAt(i);
            }
            return new Blob([ab], {type: mimeString});
        }

        function uploadImage(file) {
            var formData = new FormData();
            formData.append('file', file);

            $.ajax({
                url: 'http://60.217.22.201:8501/api/video/upload', // 后端接口 URL
                type: 'POST',
                data: formData,
                processData: false,
                contentType: false,
                success: function(response) {
                    console.log('上传成功:', response);
                },
                error: function(error) {
                    console.error('上传失败:', error);
                }
            });
        }
    });*/
</script>
<script>
    /*$("#weui-btn_update").click(function(){
        var name = $("#name").val();
        console.log(name.length);
        if(name.length <= 0 || name.length > 8){
            $.toast('请检查姓名', "forbidden");
            return false;
        }
        var tel = $("#tel").val();
        if(tel.length <= 0 || tel.length != 11){
            $.toast('请检查联系方式', "forbidden");
            return false;
        }

        // 提交表单数据
        var formData = new FormData();
        formData.append('name', name);
        formData.append('tel', tel);

        $.ajax({
            url: 'http://60.217.22.201:8501/api/video/upload', // 后端接口 URL
            type: 'POST',
            data: formData,
            processData: false,
            contentType: false,
            success: function(response) {
                console.log('提交成功:', response);
            },
            error: function(error) {
                console.error('提交失败:', error);
            }
        });
    });*/
</script>
</body>
</html>